package com.wss.interceptor;

import com.wss.domain.Account;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
    /**
     * 该方法将在Controller处理之前进行调用，return false则请求中止
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        // 获取请求的url
        String url = request.getRequestURI();
        String contextPath = request.getContextPath();
        String realPath = request.getSession().getServletContext().getRealPath("/upload");
        String indexAdmin = "/indexAdmin";
        String indexMain = "/indexMain";
//        System.out.println("RequestURI:"+url);// "/itcourseswebsite/indexMain"
//        System.out.println("ContextPath:"+ContextPath);// "/itcourseswebsite"
//        System.out.println("realPath:"+realPath);// ""
        System.out.println("=============进入拦截器=============");
        HttpSession session = request.getSession();
        Account account = (Account) session.getAttribute("Account");
        // 是否已经有用户对象在session中
        if (account != null) {
            System.out.println("==============User对象在session中=============");
            if (StringUtils.equals(url, contextPath + indexAdmin) && account.getP_id() == 1){
                System.out.println("==============用户为管理员权限允许访问admin页面=============");
                return true;
            }else if (StringUtils.equals(url, contextPath + indexAdmin)){
                System.out.println("==============用户为普通用户不允许访问admin页面=============");
                System.out.println("==============转发到loginAdmin=============");
                response.sendRedirect(contextPath + "/login/toLoginAdmin");
                return false;
            }else{
                System.out.println("==============允许访问此请求=============");
                return true;
            }
        }else if (StringUtils.equals(url, contextPath + indexMain)) {
            // 如果请求路径是indexMain或是就跳转到loginUser
            System.out.println("==============转发到loginUser=============");
            response.sendRedirect(contextPath + "/login/toLoginUser");
            return false;
        }else if (StringUtils.equals(url, contextPath + indexAdmin)) {
            // 如果请求路径是indexAdmin或是就跳转到loginAdmin
            System.out.println("==============转发到loginAdmin=============");
            response.sendRedirect(contextPath + "/login/toLoginAdmin");
            return false;
        }else{
            System.out.println("==============判断条件失败转发到loginUser=============");
            response.sendRedirect(contextPath + "/login/toLoginUser");
            return false;
        }
    }

    /**
     * Controller处理之后，ModelAndView返回之前调用这个方法
     */
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    }

    /**
     * 在ModelAndView视图渲染之后调用这个方法，一般此方法用于清理资源
     */
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    }
}
